package cn.qingyun.gis.modules.enterprise.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.enterprise.entity.Building;
import cn.qingyun.gis.modules.enterprise.entity.Grid;
import cn.qingyun.gis.modules.enterprise.entity.LayerData;
import cn.qingyun.gis.modules.enterprise.entity.dto.BuildingDto;
import cn.qingyun.gis.modules.enterprise.entity.dto.EnterpriseTableDataDto;
import cn.qingyun.gis.modules.enterprise.entity.dto.LayerDto;
import cn.qingyun.gis.modules.enterprise.entity.dto.OverViewExcelDto;
import cn.qingyun.gis.modules.enterprise.mapper.BuildingMapper;
import cn.qingyun.gis.modules.enterprise.mapper.EnterpriseMapper;
import cn.qingyun.gis.modules.enterprise.mapper.LayerDataMapper;
import cn.qingyun.gis.modules.enterprise.request.BuildingRequest;
import cn.qingyun.gis.modules.enterprise.request.OverviewRequest;
import cn.qingyun.gis.modules.enterprise.response.BuildingResponse;
import cn.qingyun.gis.modules.enterprise.response.EnterpriseOverviewResponse;
import cn.qingyun.gis.modules.enterprise.response.LayerResponse;
import cn.qingyun.gis.modules.enterprise.service.IBuildingService;
import cn.qingyun.gis.modules.enterprise.service.IEnterpriseService;
import cn.qingyun.gis.modules.enterprise.service.IGridService;
import cn.qingyun.gis.modules.enterprise.service.ILayerDataService;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * <p>
 * 五大建筑场景表 服务实现类
 * </p>
 *
 * @author skb
 * @since 2024-03-29
 */
@Service
@Slf4j
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements IBuildingService {

    @Autowired
    private IEnterpriseService enterpriseService;
    @Autowired
    private ILayerDataService layerDataService;
    @Autowired
    private LayerDataMapper layerDataMapper;
    @Autowired
    private IGridService gridService;

    @Override
    public List<BuildingResponse> getBuildingList(BuildingRequest buildingRequest) {
        List<String> types = buildingRequest.getTypes();
        List<BuildingResponse> list = new ArrayList<>();
        if (Objects.isNull(types)) {
            throw new CommonException("建筑类型不能为空");
        }
        for (String type : types) {
            QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().select(Building::getId, Building::getName, Building::getLon, Building::getLat, Building::getWkt, Building::getType, Building::getIsCover,Building::getSubCategory).eq(Building::getType, type).eq(Building::getGridCode, buildingRequest.getCode());
            List<Building> buildingList = this.list(queryWrapper);
            List<BuildingDto> buildingDtos = BeanUtil.copyToList(buildingList, BuildingDto.class);
            BuildingResponse buildingResponse = new BuildingResponse();
            buildingResponse.setType(type).setList(buildingDtos);
            list.add(buildingResponse);
        }
        return list;
    }

    @Override
    @SneakyThrows
    public void exportExcel(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        String fileName = "数据概览汇总_";
        response.setHeader("content-disposition", String.format("attachment;filename=%s.xlsx", new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1) + DateUtil.now()));

        try (ExcelWriter excelWriter = EasyExcelFactory.write(response.getOutputStream()).build()) {
            // 导出数据清单
            WriteSheet writeSheet = EasyExcelFactory.writerSheet(0, "数据概览汇总").head(OverViewExcelDto.class).build();
            QueryWrapper<Grid> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().select(Grid::getCode,Grid::getName).in(Grid::getParentCode,"SD.LB.0b","SD.LB.0a","SD.LB.0d","SD.LB.0f").or().in(Grid::getCode,"531.0012.101", "531.0013.c03", "531.0014.d02", "531.0016.0010");
            List<Grid> gridList = gridService.list(queryWrapper);
            List<OverViewExcelDto> list = new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            map.put("1","酒店");
            map.put("2","楼宇");
            map.put("3","园区");
            map.put("4","专业市场");
            map.put("5","沿街商铺");
            map.put("11","其他");
            for (Grid grid : gridList) {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    OverViewExcelDto overViewExcelDto = new OverViewExcelDto();
                    OverviewRequest overviewRequest = new OverviewRequest();
                    overviewRequest.setType("grid").setCode(grid.getCode()).setTypes(Arrays.asList(entry.getKey()));
                    EnterpriseOverviewResponse overviewData = enterpriseService.getOverviewData(overviewRequest);
                    BeanUtils.copyProperties(overviewData, overViewExcelDto);
                    overViewExcelDto.setName(grid.getName());
                    overViewExcelDto.setCode(grid.getCode());
                    overViewExcelDto.setType(entry.getKey());
                    overViewExcelDto.setTypeName(entry.getValue());
                    list.add(overViewExcelDto);
                }
            }
            excelWriter.write(list, writeSheet);
        }
    }
}
